﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace kiosk
{
    public partial class MainForm : Form
    {
        private DataBaseHandler m_DHandler;
        private List<Glass.GlassButton> m_GBttn = new List<Glass.GlassButton>();
        private TaskbarHide.FormState fm;
       
        private static Int32 msgTickCount = 0;

        
      

        public MainForm()
        {
            
            InitializeComponent();
            this.m_DHandler = new DataBaseHandler();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            if (-1 == GeneralOpt.getLanguageButtons())
            {
                MessageBox.Show("No language data");

            }
            //if(ApplicationCache.AppFormDisplayButton != null)
            //{
            //   ApplicationCache.AppFormDisplayButton.Show();
            //   //ApplicationCache.AppFormDisplayButton.Hide();
            //}

            //
            //fm = new TaskbarHide.FormState();
            //fm.Maximize(this);   
            //
        
        }



        private void reloadBtn()
        {


            int count;
            int width = ApplicationCache.mWidth;
            if (this.m_GBttn != null)
            {
                this.m_GBttn.Clear();
                this.m_GBttn = null;
                this.m_GBttn = new List<Glass.GlassButton>();
                
            
            }


            this.btnLayout.Controls.Clear();

            if (GeneralOpt.DTable.Rows.Count >= 1)
                foreach (DataRow row in GeneralOpt.DTable.Rows)
                {
                    this.m_GBttn.Add(new Glass.GlassButton());
                    count = this.m_GBttn.Count - 1;
                    this.m_GBttn[count].Text = (String)row["LanName"];
                    this.m_GBttn[count].Click += new EventHandler(GBtnClick);
                    this.m_GBttn[count].Tag = row["LanID"];
                    this.m_GBttn[count].Size = new System.Drawing.Size(width, ApplicationCache.mHeight);
                    this.m_GBttn[count].Margin = new System.Windows.Forms.Padding(ApplicationCache.mLeftMargin, ApplicationCache.mTopMargin, ApplicationCache.mRightMargin, ApplicationCache.mButtomMargin);
                    this.m_GBttn[count].Font = new Font("Arial", 15, FontStyle.Regular);
                    this.btnLayout.Controls.Add(this.m_GBttn[count]);


                }

        
        
        
        
        
        }





        
        private void MainForm_Load(object sender, EventArgs e)
        {
            int count;
            int width = ApplicationCache.mWidth;
            if (GeneralOpt.DTable.Rows.Count >=1)
            foreach (DataRow row in GeneralOpt.DTable.Rows)
            {
                this.m_GBttn.Add(new Glass.GlassButton());
                count = this.m_GBttn.Count - 1;
                this.m_GBttn[count].Text = (String)row["LanName"];
                this.m_GBttn[count].Click += new EventHandler(GBtnClick);
                this.m_GBttn[count].Tag =  row["LanID"];
                this.m_GBttn[count].Size = new System.Drawing.Size(width, ApplicationCache.mHeight);
                this.m_GBttn[count].Margin = new System.Windows.Forms.Padding(ApplicationCache.mLeftMargin, ApplicationCache.mTopMargin, ApplicationCache.mRightMargin,ApplicationCache.mButtomMargin);
                this.m_GBttn[count].Font = new Font("Arial", 15, FontStyle.Regular);
                this.btnLayout.Controls.Add(this.m_GBttn[count]);
 
            
            }




        }

        private void GBtnClick(object sender, EventArgs e)
        {

            Glass.GlassButton btn = (Glass.GlassButton)sender;
            int lanid = (int)btn.Tag;
            try
            {
                Int32 gid = this.m_DHandler.getFirstLanGroupID(lanid);
                DataTable items = this.m_DHandler.getAllGroupItems(gid);
                if (gid == -1 || (items.Rows.Count < 1))
                {
                    // no data present in the database
                    GBTextInput gbtext = new GBTextInput();
                    gbtext.ShowDialog();
                    return;

                }

                this.TopMost = false;
                DisplayButtons butt = new DisplayButtons(gid, this.m_DHandler);
                GeneralOpt.pictIDChange = -1;
               butt.ShowDialog();
                this.TopMost = true;
                if(GeneralOpt.pictIDChange != -1)
                {
                    this.Refresh();
                
                }

                //this.Focus();
                
            
            
            
            }
            catch(Exception ex)
            {

                MessageBox.Show(" There was an error " + ex.Message);
            
            
            }

        
        
        }


        #region keyHandlers 


        // put code here
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {


            if (keyData == Keys.F1)
            {
                this.TopMost = false;
                AboutBox1 ab = new AboutBox1();
                ab.ShowDialog();
                this.TopMost = true;


            }
            if (keyData == Keys.F7)
            {
                this.TopMost = false;
                GobalMessages message = new GobalMessages();
                message.ShowDialog();
                // update the Message Data
                this.setMsgCount();

                this.TopMost = true;


            }

            if (keyData == Keys.F3)
            {
                this.TopMost = false;
                GBTextInput gButtonInput = new GBTextInput();
                gButtonInput.ShowDialog();
                this.TopMost = true;
              
            
            
            
            }



            if (keyData == Keys.F2)
            {
               
                this.TopMost = false;
                this.fm.Restore(this);

            }

           

            if (keyData == Keys.F8)
            {
                this.TopMost = false;
                BackGroundImages bg = new BackGroundImages();
                bg.ShowDialog();
                MessageBox.Show("Please wait while for the changes to take place");
                if (GeneralOpt.pictIDChange != -1)
                { 
                  
                            if(ApplicationCache.MainFormBGImage != null)
                            ApplicationCache.MainFormBGImage.Dispose();
                            ApplicationCache.LoadBgImages();
                            GeneralOpt.pictIDChange = -1;
                            this.Invalidate();
                           // this.msgTickerPictBox.Invalidate();
                            this.mainLayout.Invalidate();
                            this.tableLayoutPanel2.Invalidate();
                          
                            this.btnLayout.Invalidate();
                          
                    this.TopMost = true;
                    }


               // this.TopMost = true;
                
            }

            if(keyData == Keys.F5)
            {
                // Show the margin window
                this.TopMost = false;
                Margin_Window margnWin = new Margin_Window(1);
                margnWin.ShowDialog();
                this.TopMost = true;
                ApplicationCache.loadMarginsSettings();
                this.reloadBtn();


            
            
            }


            return base.ProcessCmdKey(ref msg, keyData);
        }


       

        #endregion


        #region publicMethods

        public RectangleF getLayoutSize()
        {
           
            //RectangleF rect = new RectangleF();
            //rect =  this.mainLayout.ClientRectangle;
            return Screen.PrimaryScreen.WorkingArea;
            
            //return rect;
        
        
        
        
        }


        #endregion

        private void MainForm_Paint(object sender, PaintEventArgs e)
        {



            Graphics g = e.Graphics;



            if (ApplicationCache.MainFormBGImage != null)
            {
                g.DrawImage(ApplicationCache.MainFormBGImage, new Rectangle(0, 0, this.Width, this.Height),
                    0, 0, ApplicationCache.MainFormBGImage.Width, ApplicationCache.MainFormBGImage.Height, GraphicsUnit.Pixel);

            }
          



        }

    
      
        private void MainForm_Shown(object sender, EventArgs e)
        {

            fm = new TaskbarHide.FormState();
            fm.Maximize(this);
            this.msgTimer.Enabled = true;


        }

    
        private void mainLayout_Paint(object sender, PaintEventArgs e)
        {


            Point p = this.mainLayout.PointToScreen(new Point(this.mainLayout.ClientRectangle.X, this.mainLayout.ClientRectangle.Y));
            Rectangle screenRect = new Rectangle(p.X, p.Y, this.mainLayout.ClientRectangle.Width, this.mainLayout.ClientRectangle.Height);

            if (ApplicationCache.MainFormBGImage != null)
            {
                // Bitmap nBit = ((Bitmap)ApplicationCache.MainFormBGImage).Clone(tt, ApplicationCache.MainFormBGImage.PixelFormat);
                Graphics g = e.Graphics;

                g.DrawImage(ApplicationCache.MainFormBGImage, this.mainLayout.ClientRectangle,
                    screenRect.X, screenRect.Y, screenRect.Width, screenRect.Height, GraphicsUnit.Pixel);
                //nBit.Dispose();
            }



        }

        private void tableLayoutPanel2_Paint(object sender, PaintEventArgs e)
        {

            Point p = this.tableLayoutPanel2.PointToScreen(new Point(this.tableLayoutPanel2.ClientRectangle.X, this.tableLayoutPanel2.ClientRectangle.Y));
            Rectangle screenRect = new Rectangle(p.X, p.Y, this.tableLayoutPanel2.ClientRectangle.Width, this.tableLayoutPanel2.ClientRectangle.Height);

            // Create a clone
            if (ApplicationCache.MainFormBGImage != null)
            {
                //Bitmap bit = ((Bitmap)ApplicationCache.MainFormBGImage).Clone(tt, ApplicationCache.MainFormBGImage.PixelFormat);


                Graphics g = e.Graphics;

                g.DrawImage(ApplicationCache.MainFormBGImage, this.tableLayoutPanel2.ClientRectangle,
                    screenRect.X, screenRect.Y, screenRect.Width, screenRect.Height, GraphicsUnit.Pixel);
                //bit.Dispose();
            
            }
            

            



        }

        private void btnLayout_Paint(object sender, PaintEventArgs e)
        {




            Point p = this.btnLayout.PointToScreen(new Point ( this.btnLayout.ClientRectangle.X, this.btnLayout.ClientRectangle.Y));
            Rectangle screenRect = new Rectangle(p.X, p.Y, this.btnLayout.ClientRectangle.Width, this.btnLayout.ClientRectangle.Height);

            if (ApplicationCache.MainFormBGImage != null)
            {
               // Bitmap nBit = ((Bitmap)ApplicationCache.MainFormBGImage).Clone(tt, ApplicationCache.MainFormBGImage.PixelFormat);
                Graphics g = e.Graphics;

                g.DrawImage(ApplicationCache.MainFormBGImage, this.btnLayout.ClientRectangle,
                    screenRect.X, screenRect.Y, screenRect.Width, screenRect.Height, GraphicsUnit.Pixel);
                //nBit.Dispose();
            }


            

            
        }

        private void tickerMsgLabel_Paint(object sender, PaintEventArgs e)
        {



            //Point p = this.tickerMsgLabel.PointToScreen(new Point(this.tickerMsgLabel.ClientRectangle.X, this.tickerMsgLabel.ClientRectangle.Y));
            //Rectangle screenRect = new Rectangle(p.X, p.Y, this.tickerMsgLabel.ClientRectangle.Width, this.tickerMsgLabel.ClientRectangle.Height);

            //if (ApplicationCache.MainFormBGImage != null)
            //{
            //    // Bitmap nBit = ((Bitmap)ApplicationCache.MainFormBGImage).Clone(tt, ApplicationCache.MainFormBGImage.PixelFormat);
            //    Graphics g = e.Graphics;

            //    g.DrawImage(ApplicationCache.MainFormBGImage, this.tickerMsgLabel.ClientRectangle,
            //        screenRect.X, screenRect.Y, screenRect.Width, screenRect.Height, GraphicsUnit.Pixel);
            //    //nBit.Dispose();
            //}



        }

        private void msgTickerPictBox_Paint(object sender, PaintEventArgs e)
        {

            //Point p = this.msgTickerPictBox.PointToScreen(new Point(this.msgTickerPictBox.ClientRectangle.X, this.msgTickerPictBox.ClientRectangle.Y));
            //Rectangle screenRect = new Rectangle(p.X, p.Y, this.msgTickerPictBox.ClientRectangle.Width, this.msgTickerPictBox.ClientRectangle.Height);

            //if (ApplicationCache.MainFormBGImage != null)
            //{
            //    // Bitmap nBit = ((Bitmap)ApplicationCache.MainFormBGImage).Clone(tt, ApplicationCache.MainFormBGImage.PixelFormat);
            //    Graphics g = e.Graphics;

            //    g.DrawImage(ApplicationCache.MainFormBGImage, this.msgTickerPictBox.ClientRectangle,
            //        screenRect.X, screenRect.Y, screenRect.Width, screenRect.Height, GraphicsUnit.Pixel);
            //    //nBit.Dispose();
            //}
            //if(this.msgTicker != String.Empty)
            //{

            //    TextRenderer.DrawText(e.Graphics,this.msgTicker,new Font("Arial", 10, FontStyle.Regular), new Point(0, this.msgTickerPictBox.Height / 2), Color.White);
            
            
            //}




        }

     
      

        private void msgTimer_Tick(object sender, EventArgs e)
        {

           
            // Get the messages from the DataBase server
            if (ApplicationCache.TickerMessages.Count >= 1 && MainForm.msgTickCount < ApplicationCache.TickerMessages.Count && ApplicationCache.TickerMessages != null)
            {
                TkmsgBtn.Text  = ApplicationCache.TickerMessages[msgTickCount].ItemText;
                MainForm.msgTickCount =  ++MainForm.msgTickCount % (ApplicationCache.TickerMessages.Count);

               
            
            }




        }

        private void setMsgCount()
        { 
        
            this.msgTimer.Enabled = false;
            if (ApplicationCache.TickerMessages != null)
            {
                ApplicationCache.TickerMessages.Clear();
                ApplicationCache.TickerMessages = null;
            }
            ApplicationCache.TickerMessages = this.m_DHandler.getGMSG();
            this.msgTimer.Enabled = true;
        
        
        }

        private void webBtn_Click(object sender, EventArgs e)
        {

            this.TopMost = false;
            webPage wPage = new webPage();
            wPage.ShowDialog();
            this.TopMost = true;

        }

     

    }
}
